namespace Sckf.MicroService.Services.Catalog.Services;

/// <summary>
/// Catalog服务(Command)
/// </summary>
public partial class CatalogService : ServiceBase
{
    /// <summary>
    /// 创建分类
    /// </summary>
    /// <param name="eventBus"></param>
    /// <param name="command"></param>
    /// <returns></returns>
    [RoutePattern(pattern: "create", true)]
    public async Task<IResult> CreateCatalogAsync([FromServices] IEventBus eventBus, CreateCatalogCommand command)
    {
        await eventBus.PublishAsync(command);
        return Results.Accepted();
    }

    [RoutePattern(pattern: "update", true)]
    public async Task<IResult> UpdateCatalogAsync([FromServices] IEventBus eventBus, UpdateCatalogCommand command)
    {
        await eventBus.PublishAsync(command);
        return Results.Accepted();
    }

    [RoutePattern(pattern: "delete", true)]
    public async Task<IResult> DeleteCatalogAsync([FromServices] IEventBus eventBus, [FromBody] DeleteCatalogCommand command)
    {
        await eventBus.PublishAsync(command);
        return Results.Accepted();
    }
}

/// <summary>
/// Catalog服务(Query)
/// </summary>
public partial class CatalogService
{

    [RoutePattern(pattern: "detail", true)]
    public async Task<IResult> GetAsync(IEventBus eventBus, long catalogId)
    {
        var query = new CatalogQuery
        {
            CatalogId = catalogId
        };
        await eventBus.PublishAsync(query);
        if (query.Result is null)
        {
            return Results.NotFound();
        }
        else
        {
            return Results.Ok(query.Result.Map<Dto.CatalogItemDto>());
        };
    }

    [RoutePattern(pattern: "list", true)]
    public async Task<IResult> GetItemsAsync(IEventBus eventBus, int pageIndex = 0, int pageSize = 20, string keyword = "")
    {
        var query = new CatalogsQuery()
        {
            PageIndex = pageIndex,
            PageSize = pageSize,
            Keyword = keyword
        };
        await eventBus.PublishAsync(query);

        return Results.Ok(query.Result.Map<PaginatedResultViewModel<Dto.CatalogListDto>>());
    }
}


public partial class CatalogService
{
    public CatalogService()
    {
        ServiceName = "catalog";
    }
}